package io.renren.modules.collect.controller;

import java.util.List;
import java.util.Map;

import io.renren.common.annotation.SysLog;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.modules.collect.entity.CollectinfosCustomEntity;
import io.renren.modules.projectinfo.service.ProjectinfosService;
import io.renren.modules.user.service.UserinfosService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.renren.modules.collect.entity.CollectinfosEntity;
import io.renren.modules.collect.service.CollectinfosService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;


/**
 * 
 * 
 * @author 
 * @email 
 * @date 2018-10-24 18:48:18
 */
@RestController
@RequestMapping("/collect/collectinfos")
public class CollectinfosController {
	@Autowired
	private CollectinfosService collectinfosService;

	@Autowired
	private UserinfosService userinfosService;

	@Autowired
	private ProjectinfosService projectinfosService;

	/**
	 * 列表
	 */
	@SysLog("查询收藏列表")
	@RequestMapping("/list")
	@RequiresPermissions("collect:collectinfos:list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<CollectinfosCustomEntity> collectinfosList = collectinfosService.queryCustomList(query);
		int total = collectinfosService.queryTotalByUser(query);
		PageUtils pageUtil = new PageUtils(collectinfosList, total, query.getLimit(), query.getPage());
		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@SysLog("查询收藏信息")
	@GetMapping("/info/{collectinfoid}")
	@RequiresPermissions("collect:collectinfos:info")
	public R info(@PathVariable("collectinfoid")String collectinfoid){
		CollectinfosEntity collectinfos = collectinfosService.queryCustomInfo(collectinfoid);
		return R.ok().put("collectinfos", collectinfos);
	}
	
	/**
	 * 保存
	 */
	@SysLog("保存收藏信息")
	@PostMapping("/save")
	@RequiresPermissions("collect:collectinfos:save")
	public R save(@RequestBody CollectinfosEntity collectinfos){
		//添加pojo属性验证
		ValidatorUtils.validateEntity(collectinfos, AddGroup.class);
		//外键判空
		//1.判断项目是否收藏过，1.用户是否存在，2.项目是否存在
		R existCollect = collectinfosService.isExistCollect(collectinfos);
		if(existCollect != null){
			return existCollect;
		}
		collectinfosService.save(collectinfos);
		//添加收藏次数
		projectinfosService.addCollectTimes(collectinfos.getTargetid());
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@SysLog("修改收藏信息")
	@PostMapping("/update")
	@RequiresPermissions("collect:collectinfos:update")
	public R update(@RequestBody CollectinfosEntity collectinfos){
		//添加pojo属性验证
		ValidatorUtils.validateEntity(collectinfos, AddGroup.class);
		//外键判空
		//用户
		//判断是否已经存在此收藏
		R existCollect = collectinfosService.isExistCollect(collectinfos);
		if(existCollect != null){
			return existCollect;
		}
		//减一项目收藏次数
		//获取项目
		CollectinfosEntity collectinfosEntity = collectinfosService.queryObject(collectinfos.getCollectinfoid());
		projectinfosService.minusCollectTimes(collectinfosEntity.getTargetid());
		collectinfosService.update(collectinfos);
		//加一项目收藏次数
		projectinfosService.addCollectTimes(collectinfos.getTargetid());
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@SysLog("删除收藏信息")
	@PostMapping("/delete")
	@RequiresPermissions("collect:collectinfos:delete")
	public R delete(@RequestBody String[] collectinfoids){
		collectinfosService.deleteBatch(collectinfoids);
		return R.ok();
	}
	
}
